<template>
    <el-form ref="form"  label-width="100px">
        <el-form-item label="待复制内容" label-width="100px">
            <el-input auto-complete="off" :value="content"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" :loading="loading" @click="submit($event)">点我复制</el-button>
            <el-button @click="cancel">返回</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
import Clipboard from 'clipboard';

export default {
        props: {
            data: {
                type: String,
                default: '',
            },
        },
        data () {
            return {
                loading: false,
                content: '',
            };
        },
        computed: {},
        watch: {
            data (newVal) {
                this.syncForm();
            }
        },
        methods: {
            submit(event) {
                const cb = new Clipboard('.t', {
                    text: () => this.content,
                });
                cb.on('success', (e) => {
                    cb.off('error');
                    cb.off('success');
                    this.$message.success('复制成功');
                    this.$emit('submit');

                });
                cb.on('error', (e) => {
                    cb.off('error');
                    cb.off('success');
                    console.error('clipboardCopy error', e);
                    this.$message.warning('复制失败');
                });
                cb.onClick(event);
            },
            cancel() {
                this.$emit('cancel');
            },
            syncForm() {
                this.content = this.data;
            }
        },
        created() {
            this.syncForm();
        }
    }
</script>

<style scoped>

</style>
